//
//  GRPayOrderDatailVC.m
//  YC_GRice
//
//  Created by luojinrong11 on 2018/9/4.
//  Copyright © 2018年 YeCai. All rights reserved.
//

#import "GRPayOrderDatailVC.h"
#import "GRPayDetailTableViewCell.h"
#import "GRStoreAction.h"
#import "ReturnIntegralView.h"
#import "GRMyBillModel.h"
#import "GROrderCommentViewController.h"
#import "GRPayBillViewController.h"
@interface GRPayOrderDatailVC ()<UITableViewDelegate,UITableViewDataSource>
{
    BOOL isShow;
}
@property (strong, nonatomic) IBOutlet UIView *headerView;
@property (strong, nonatomic) IBOutlet UIView *footView;

@property (weak, nonatomic) IBOutlet UITableView *PayDetailList;
@property(nonatomic,strong)NSMutableArray *payArr;

@property (weak, nonatomic) IBOutlet UILabel *priceLabel;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property(nonatomic,strong)GRMyBillModel * billModel;
@property(nonatomic,strong)ReturnIntegralView *intergralView;
@end

@implementation GRPayOrderDatailVC
-(ReturnIntegralView *)intergralView {
    if (!_intergralView) {
        _intergralView = [[ReturnIntegralView alloc]init];
    }
    return _intergralView;
}


-(BOOL)navigationShouldPopOnBackButton{
    BOOL isPay = NO;
    for (UIViewController * viewControlller in self.navigationController.viewControllers) {
        if([ viewControlller isKindOfClass:[GRPayBillViewController class]]){
            isPay = YES;
        }
    }
    if (isPay) {
        
        [self.navigationController popToViewController:self.navigationController.viewControllers[self.navigationController.viewControllers.count -3] animated:YES];
        return NO;
    }else{
        return YES;
    }
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.barTintColor = KGMTHEM_COLOR;
}

-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self.PayDetailList registerNib:[UINib nibWithNibName:@"GRPayDetailTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"payDetailCell"];
    self.PayDetailList.delegate = self;
    self.PayDetailList.dataSource = self;
    self.PayDetailList.tableHeaderView = self.headerView;
    self.PayDetailList.tableFooterView = [[UIView alloc]init];
    [self requestData];
}

-(void)requestData{
    [GRStoreAction getpayDetails:self.orderId ComplectionHander:^(BOOL success, id responseObject, GRError *error) {
        if (success) {
            NSDictionary * dic = responseObject;
            self.billModel = [GRMyBillModel yy_modelWithDictionary:dic];
            self.nameLabel.text = dic[@"nikeName"];
            self.priceLabel.text = [NSString stringWithFormat:@"%.2lf",[dic[@"totalMoney"]  floatValue]];
            self.intergralView.integral =[NSString stringWithFormat:@"%.2lf",[dic[@"addPoint"]  floatValue]];
            if (_isPay&&!isShow &&[dic[@"addPoint"]  floatValue] >0 ) {
                isShow = YES;
                [self.intergralView showIntegral];
            }
            if ([dic[@"isComment"] boolValue]) {
                self.PayDetailList.tableFooterView = self.footView;
            }else{
                self.PayDetailList.tableFooterView = [[UIView alloc]init];
            }
            self.payArr = [NSMutableArray array];
            NSArray * payArr = @[
                                 @{@"leftTitle":@"消费金额",@"rightText":[NSString stringWithFormat:@"￥%.2lf",[dic[@"totalMoney"]  floatValue]],@"textColor":HEXCOLOR(0x222222)},
                                 @{@"leftTitle":@"龟米红包",@"rightText":[dic[@"shardBountyMoney"] isKindOfClass:[NSNull class]]?@"-￥0":[NSString stringWithFormat:@"-￥%.2lf",[dic[@"shardBountyMoney"]  floatValue]],@"textColor":HEXCOLOR(0x222222)},
                                 @{@"leftTitle":@"赠送额度",@"rightText":[NSString stringWithFormat:@"-￥%.2lf",[dic[@"giveDiscount"]  isKindOfClass:[NSNull class]]?0:[dic[@"giveDiscount"]  floatValue]],@"textColor":HEXCOLOR(0x222222)},
                                 
                                 @{@"leftTitle":@"优惠券",@"rightText":[NSString stringWithFormat:@"-￥%.2lf",[dic[@"voucherMoney"]  isKindOfClass:[NSNull class]]?0:[dic[@"voucherMoney"]  floatValue]],@"textColor":HEXCOLOR(0x222222)},
                                 
                                 @{@"leftTitle":@"龟米优惠",@"rightText":[NSString stringWithFormat:@"-￥%.2lf",[dic[@"costPoint"]  floatValue]],@"textColor":HEXCOLOR(0x222222)},
                                 @{@"leftTitle":@"实付金额",@"rightText":[NSString stringWithFormat:@"￥%.2lf",[dic[@"payMoney"]  floatValue]],@"textColor":HEXCOLOR(0x222222)},
                                 @{@"leftTitle":@"消费时间",@"rightText":dic[@"paidAtStr"] ,@"textColor":HEXCOLOR(0x999999)},
                                 @{@"leftTitle":@"支付单号",@"rightText":dic[@"num"] ,@"textColor":HEXCOLOR(0x999999)},
                                 @{@"leftTitle":@"支付方式",@"rightText":  [dic[@"type"] isEqualToString:@"alipay"]?@"支付宝":[dic[@"type"] isEqualToString:@"point"]?@"龟米":@"微信" ,@"textColor":HEXCOLOR(0x999999)},
                                 @{@"leftTitle":@"手机号码",@"rightText":dic[@"mobile"],@"textColor":HEXCOLOR(0x999999)},
                                 @{@"leftTitle":@"获得龟米",@"rightText":[NSString stringWithFormat:@"+%.2lf",[dic[@"addPoint"]  floatValue]],@"textColor":HEXCOLOR(0x999999)},
                                 ];
            [self.payArr addObjectsFromArray:payArr];
         

     
            [self.PayDetailList reloadData];
        }
    }];
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    GRPayDetailTableViewCell * payDetailCell = [tableView dequeueReusableCellWithIdentifier:@"payDetailCell" forIndexPath:indexPath];
    payDetailCell.contentDic = self.payArr[indexPath.row];
    payDetailCell.selectionStyle =  UITableViewCellSelectionStyleNone;
    return payDetailCell;
}

- (IBAction)commentAction:(UIButton *)sender {
    GROrderCommentViewController * commentVC = [[GROrderCommentViewController alloc]init];
    commentVC.isRiceOrder = YES;
    commentVC.isStoreComment = YES;
    commentVC.billModel = self.billModel;
    WEAKSELF
    commentVC.successBlock = ^{
        [weakSelf requestData];
    };
    [self.navigationController pushViewController:commentVC animated:YES];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.payArr.count;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
